package com.zjmzxfzhl.modules.purchase.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.Result;
import com.zjmzxfzhl.common.base.BaseController;
import com.zjmzxfzhl.common.util.FileUtil;
import com.zjmzxfzhl.modules.purchase.controller.vo.PurchaseProjectVo;
import com.zjmzxfzhl.modules.purchase.entity.PurchaseProject;
import com.zjmzxfzhl.modules.purchase.service.PurchaseProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.util.List;

/**
 * Controller
 *
 * @author 庄金明
 */
@RestController
@RequestMapping("/purchase/project")
public class PurchaseProjectController extends BaseController {
    @Autowired
    private PurchaseProjectService purchaseProjectService;

    /**
     * 自定义查询列表
     *
     * @param purchaseProject
     * @param current
     * @param size
     * @return
     */
    @RequiresPermissions("purchase:project:list")
    @GetMapping(value = "/list")
    public Result list(PurchaseProject purchaseProject, @RequestParam Integer current, @RequestParam Integer size) {
        IPage<PurchaseProjectVo> pageList = purchaseProjectService.list(new Page<PurchaseProjectVo>(current, size), purchaseProject);
        return Result.ok(pageList);
    }

    /**
     * 查询所有项目
     *
     * @return
     */
    @RequiresPermissions("purchase:supplier:list")
    @GetMapping(value = "/listAll")
    public Result listAll() {
        List<PurchaseProject> projects = purchaseProjectService.list();
        return Result.ok(projects);
    }

    @RequiresPermissions("purchase:project:list")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        PurchaseProject purchaseProject = purchaseProjectService.getById(id);
        return Result.ok(purchaseProject);
    }

    /**
     * @param purchaseProject
     * @return
     * @功能：新增
     */
    @RequiresPermissions("purchase:project:save")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody PurchaseProject purchaseProject) {
        purchaseProjectService.save(purchaseProject);
        return Result.ok();
    }

    /**
     * @param purchaseProject
     * @return
     * @功能：修改
     */
    @RequiresPermissions("purchase:project:update")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody PurchaseProject purchaseProject) {
        purchaseProjectService.updateById(purchaseProject);
        return Result.ok();
    }

    /**
     * @param ids
     * @return
     * @功能：批量删除
     */
    @RequiresPermissions("purchase:project:delete")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        String[] idsArr = ids.split(",");
        purchaseProjectService.deleteProjects(Convert.toList(Long.class, idsArr));
        return Result.ok();
    }

    /**
     * 导出项目-采购清单
     *
     * @param response
     * @param projectId
     */
    @GetMapping(value = "/export")
    public void export(HttpServletResponse response, @RequestParam("projectId") Long projectId) {
        File exportFile = purchaseProjectService.exportDetail(projectId);
        FileUtil.setDownloadResponse(response, exportFile, exportFile.getName(), true);
    }

    @GetMapping(value = "/replaceProject")
    public Result replaceSupplier(@RequestParam("oids") String oids, @RequestParam("nid") Long nid) {
        if (StringUtils.isEmpty(oids) || oids.trim().length() == 0) {
            return Result.error("oids can't be empty");
        }
        String[] oidsArr = oids.split(",");
        List<Long> oidList = Convert.toList(Long.class, oidsArr);
        oidList.remove(nid);
        purchaseProjectService.replaceProjects(oidList, nid);
        return Result.ok();
    }
}
